package com.kingdee.pda.ui.activity;

import static com.example.basecommon.utils.LoginUtil.LOGIN_NAME;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.basecommon.utils.App;
import com.example.basecommon.utils.PreferencesUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.MyApplication;
import com.kingdee.pda.entity.PriorityBean;
import com.kingdee.pda.priority.PriorityContract;
import com.kingdee.pda.priority.PriorityPresenter;
import com.kingdee.pda.ui.set_config.BaseConfigureActivity;
import com.kingdee.pda.ui.set_config.OutWarehouseConfigureActivity;
import com.kingdee.pda.ui.set_config.SkuSplitConfigureActivity;
import com.kingdee.pda.util.WarehouseUtil;
import com.kingdee.pda.plugins.http.common.OnRequestCallback;
import com.kingdee.pda.plugins.http.serverinterface.ILogin;
import com.kingdee.pda.plugins.http.serverinterface.impl.LoginImpl;
import com.kingdee.pda.ui.dialog.LoadDialog;
import com.kingdee.pda.ui.dialog.RemindDialog;
import com.kingdee.pda.util.ConstantPreference;
import com.kingdee.pda.util.ToastUtils;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.UpgradeInfo;

/**
 * 设置
 * Created by zhanlg
 * DATE 2021/5/24
 * TIME 16:14
 */
public class SettingActivity extends Activity implements View.OnClickListener , PriorityContract.IPriorityView {

    public static final int REQUEST_CODE = 1000;
    private TextView mTvLoginName;
    private LoadDialog loadDialog;
    private TextView tv_updateMsg;
    private PriorityPresenter priorityPresenter;
    private LinearLayout ll_base_config;
    private LinearLayout ll_outcheck_config;
    private LinearLayout ll_skuSplit;


    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, SettingActivity.class);
        activity.startActivityForResult(intent, REQUEST_CODE);
        activity.overridePendingTransition(R.anim.slide_right_in, 0);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        priorityPresenter = new PriorityPresenter(this,this);
        String loginName = PreferencesUtils.getString(App.INSTANCE.getApplicationContext(), LOGIN_NAME);
        priorityPresenter.getPriorityList(loginName);
        initWindow();
        initView();

        loadUpgradeInfo();
    }

    private void loadUpgradeInfo() {

        /***** 获取升级信息 *****/
        UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();

        if (upgradeInfo == null) {
            return;
        } else {
            tv_updateMsg.setVisibility(View.VISIBLE);
        }
    }

    private void initWindow() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.RIGHT;
    }

    private void initView() {
        mTvLoginName = findViewById(R.id.tv_login_name);
        findViewById(R.id.ll_change_warehouse).setOnClickListener(this);
        findViewById(R.id.btn_logout).setOnClickListener(this);
        findViewById(R.id.ll_check_version).setOnClickListener(this);
        ll_skuSplit=findViewById(R.id.ll_skuSplit);
        ll_skuSplit.setOnClickListener(this);
        ll_base_config=findViewById(R.id.ll_base_config);
        ll_base_config.setOnClickListener(this);
        ll_outcheck_config = findViewById(R.id.ll_outcheck_config);
        ll_outcheck_config.setOnClickListener(this);

        tv_updateMsg = findViewById(R.id.tv_updateMsg);

        mTvLoginName.setText(PreferencesUtils.getString(this, ConstantPreference.LOGIN_NAME, ""));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_change_warehouse:
//                Vibrator vib = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);
//                vib.vibrate(500);
                setResult(RESULT_OK);
                finish();
                overridePendingTransition(0, R.anim.slide_right_out);
                break;
            case R.id.btn_logout:
                showLogoutRemindDialog();
                break;
            case R.id.ll_check_version:
                /***** 检查更新 *****/
                Beta.checkUpgrade();
                break;
            case R.id.ll_skuSplit:
               SkuSplitConfigureActivity.actionStartForResult(this);
                break;
            case R.id.ll_base_config:
                BaseConfigureActivity.actionStartForResult(this);
                break;
            case R.id.ll_outcheck_config:
                OutWarehouseConfigureActivity.actionStartForResult(this);
                break;
        }
    }

    private void showLogoutRemindDialog() {
        RemindDialog remindDialog = new RemindDialog(SettingActivity.this);
        remindDialog.setNoTitle();
        remindDialog.setContent("是否退出登录？");
        remindDialog.setShowCancelButton();
        remindDialog.setListener(new RemindDialog.OnConfirmClickListener() {
            @Override
            public void onConfirmClick() {
                requestLogout();
            }
        });
        remindDialog.show();
    }

    private void requestLogout() {
        showLoadingDialog("loading...");
        ILogin iLogout = new LoginImpl();
        iLogout.logout(new OnRequestCallback<String>() {
            @Override
            public void onFailedCallback(boolean isRequestCallback, final String errorMsg) {
                super.onFailedCallback(isRequestCallback, errorMsg);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoadingDialog();
                        ToastUtils.showLong(SettingActivity.this, errorMsg);
                    }
                });
            }

            @Override
            public void onSuccessCallback(String result, Object object) {
                super.onSuccessCallback(result, object);
                doLogoutClear();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoadingDialog();
                        MyApplication.getInstance().removeActivity(MainActivity.class.getName());
                        LoginActivity.actionStart(SettingActivity.this);
                        finish();
                        overridePendingTransition(0, R.anim.slide_right_out);
                    }
                });
            }
        });
    }

    private void doLogoutClear() {
//        LoginUtil.clearLoginInfo();
        WarehouseUtil.clearWarehouseInfo();
    }

    public void showLoadingDialog(String content) {
        if (loadDialog == null) {
            loadDialog = new LoadDialog(SettingActivity.this);
        }
        if (!loadDialog.isShowing() && !isDestroyed()) {
            loadDialog.show();
            loadDialog.setTextView(content);
        }
    }

    public void dismissLoadingDialog() {
        if (loadDialog != null && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.slide_right_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        overridePendingTransition(0, R.anim.slide_right_out);
    }

    @Override
    public void getPrioritySuccess(PriorityBean.Data priorityBean) {
        updateView(priorityBean);

    }

    @Override
    public void getPriorityFailed(String msg) {

    }

    private void updateView(PriorityBean.Data priority){
        if (priority.basicset != null && priority.basicset.size() > 0) {
            for (PriorityBean.Data.ModulePriority stock: priority.basicset) {
                if (TextUtils.equals(stock.number, "basicconfig")){
                    ll_base_config.setVisibility(View.VISIBLE);
                }

                if (TextUtils.equals(stock.number, "issueconfig")){
                    ll_outcheck_config.setVisibility(View.VISIBLE);
                }

                if (TextUtils.equals(stock.number, "commoconfig")){
                    ll_skuSplit.setVisibility(View.VISIBLE);
                }
            }
        }


    }

}
